package com.facebook.app.shared.clustering;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

public class Centroid implements Clusterable, Serializable {

	private static final long serialVersionUID = -4079086305214324710L;

	private List<Clusterable> clusterElements = new LinkedList<Clusterable>();
	private transient double[] characteristicClusterVector;
	// may be transient because the k-means clustering is not
	// split at the moment

	public Centroid(double[] characteristicClusterVector) {
		this.characteristicClusterVector = characteristicClusterVector;
	}

	@SuppressWarnings("unused")
	private Centroid() {
	}

	public List<Clusterable> getClusterElements() {
		return clusterElements;
	}

	@Override
	public double[] getCharacteristicClusterVector() {
		return characteristicClusterVector;
	}

	@Override
	public String toString() {
		return "Centroid [clusterElements=" + clusterElements + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + Arrays.hashCode(characteristicClusterVector);
		result = prime * result
				+ ((clusterElements == null) ? 0 : clusterElements.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Centroid other = (Centroid) obj;
		if (!Arrays.equals(characteristicClusterVector,
				other.characteristicClusterVector))
			return false;
		if (clusterElements == null) {
			if (other.clusterElements != null)
				return false;
		} else if (!clusterElements.equals(other.clusterElements))
			return false;
		return true;
	}
	
	public void addElementAndAdaptVector(Clusterable element) {
		
		if (element != null) {
			clusterElements.add(element);
			
			adaptCharacteristicClusterVectorToClusterElements();
		}
	}
	
	public void addAllElementsAndAdaptVector(Collection<Clusterable> elements) {
		
		if (elements != null) {
			clusterElements.addAll(elements);
			
			adaptCharacteristicClusterVectorToClusterElements();
		}
	}
	
	public void removeElementAndAdaptVector(Clusterable element) {
		
		if (element != null) {
			clusterElements.remove(element);
			
			adaptCharacteristicClusterVectorToClusterElements();
		}
	}
	
	public void removeAllElementsAndAdaptVector(Collection<Clusterable> elements) {
		
		if (elements != null) {
			clusterElements.removeAll(elements);
			
			adaptCharacteristicClusterVectorToClusterElements();
		}
	}
	
	public void adaptCharacteristicClusterVectorToClusterElements() {

		if (clusterElements.size() == 0) {
			// "reset"
			characteristicClusterVector = new double[characteristicClusterVector.length];
			return;
		}

		for (int i = 0; i < characteristicClusterVector.length; i++) {
			double sum = 0;

			for (Clusterable element : clusterElements) {
				sum += element.getCharacteristicClusterVector()[i];
			}
			characteristicClusterVector[i] = sum / clusterElements.size();
		}

		// for (int i = 0; i < characteristicClusterVector.length; i++) {
		// for (Clusterable element : clusterElements) {
		// if (element.getCharacteristicClusterVector()[i] > 0) {
		// characteristicClusterVector[i] = 1;
		// break;
		// }
		// }
		// }
	}

}
